package controller;

import dao.NewsDao;
import dao.impl.NewsDaoimpl;
import entity.News;
import service.NewsService;
import service.impl.NewsServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(name = "NewServlet", urlPatterns = "/newsServlet")
public class NewServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");//获取url后缀
        NewsService newsService = new NewsServiceimpl();
        List<News> newsList = new ArrayList();
        if ("select".equals(action)) {
            newsList = newsService.getNewsList();//获取list集合
            request.setAttribute("newsList", newsList);
            request.getRequestDispatcher(request.getContextPath() + "/jsp/newsDetailList.jsp").forward(request, response);
        } else if ("add".equals(action)) {
            int categoryId = Integer.parseInt(request.getParameter("categoryId"));
            String title = request.getParameter("title");
            String author = request.getParameter("author");
            String sumary = request.getParameter("summary");
            String content = request.getParameter("newscontent");
            News news = new News();
            news.setCategoryId(categoryId);
            news.setTitle(title);
            news.setAuthor(author);
            news.setSummary(sumary);
            news.setContent(content);
            news.setCreateDate(new Date());
            int good = newsService.addNews(news);
            if (good > 0) {
                response.sendRedirect(request.getContextPath() + "/newsServlet?action=select&good=true");
            } else {
                response.sendRedirect(request.getContextPath() + "/jsp/newsDetailCreateSimple.jsp?good=false");
            }
        } else if ("del".equals(action)) {
            int id = Integer.parseInt(request.getParameter("id"));
            int good = newsService.delNews(id);
            if (good > 0) {
                response.sendRedirect(request.getContextPath() + "/newsServlet?action=select&good=true");
            } else {
                response.sendRedirect(request.getContextPath() + "/jsp/newsDetailList.jsp?good=false");
            }
        } else if ("change".equals(action)) {
            int id = Integer.parseInt(request.getParameter("id"));
            int categoryId = Integer.parseInt(request.getParameter("categoryId"));
            String title = request.getParameter("title");
            String author = request.getParameter("author");
            String sumary = request.getParameter("summary");
            String content = request.getParameter("newscontent");
            News news = new News();
            news.setId(id);
            news.setCategoryId(categoryId);
            news.setTitle(title);
            news.setAuthor(author);
            news.setSummary(sumary);
            news.setContent(content);
            news.setCreateDate(new Date());
            int good = newsService.upNews(news);
            if (good > 0) {
                response.sendRedirect(request.getContextPath() + "/newsServlet?action=select&good=true");
            } else {
                response.sendRedirect(request.getContextPath() + "/jsp/newsDetailUpdateSimple.jsp?good=false");
            }
        } else if ("get".equals(action)) {
            int id = Integer.parseInt(request.getParameter("id"));
            News news = newsService.selNews(id);
            request.setAttribute("newsByid",news);
            request.getRequestDispatcher("/jsp/newsDetailUpdateSimple.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
